<?php
/************************
 *  Codenova.pl Project
 *  Created by: Kamil Mrzygłód
 *
 *  (c) 2013 by Codenova.pl
 *  All Right Reserved
 ***********************/

namespace Gotcha\App\Extensions;

// Eclipse logs class

use Gotcha\App\GotchaBootstrap;
use Gotcha\App\Types\String;

final class GotchaLogsExtension extends GotchaBootstrap {
    static public function createNewLogEntry(String $data, String $param1 = null, String $param2 = null, String $param3 = null) {
        // Get naming format
        $log_file_name_array = explode('-', (string)parent::xmlFileHandler(new String('/App/Config/config.xml'), new String('logfilenameformat')));
        $log_file_name = '';

        foreach ($log_file_name_array as $expression) {
            switch ($expression) {
                case 'p':
                    $log_file_name .= (string)parent::xmlFileHandler(new String('/App/Config/config.xml'), new String('logfileprefix')).'_';
                    break;
                case 'd':
                    $log_file_name .= date('d').'_';
                    break;
                case 'm':
                    $log_file_name .= date('m').'_';
                    break;
                case 'y':
                    $log_file_name .= date('Y').'_';
                    break;
            }
        }

        // Remove last sign from string("_")
        $log_file_name = substr_replace($log_file_name, "", -1);

        // Debug backtrace printing
        // We use output buffering to prevent PHP
        // from printing function output
        ob_start();
        debug_print_backtrace();
        $backtrace = ob_get_contents(); // Write buffer into variable
        ob_end_clean(); // Clear buffer and turn it off

        // Clean-up backtrace
        $backtrace_array = explode('#', $backtrace);
        $backtrace = '[Zapis stosu]:'.PHP_EOL; // Clear for clean-up
        foreach ($backtrace_array as $backtrace_trace) {
            $backtrace .= '#'.$backtrace_trace.PHP_EOL;
        }

        // If we have optional params, there is a need
        // to format output data
        if ($param1 || $param2 || $param3) {
            $data = sprintf($data, $param1, $param2, $param3);
        }

        // Method appends new data to file if it exists,
        // otherwise creates new log file
        // (appending depends on global config)
        if ((string)parent::xmlFileHandler(new String('App/Config/config.xml'), new String('logfileentryformat')) === 'append') {
            file_put_contents((string)parent::xmlFileHandler(new String('App/Config/config.xml'), new String('logfiledir')).$log_file_name.'.txt', '['.date('d-m-Y H:i:s').'] '.$data.PHP_EOL.$backtrace.PHP_EOL.PHP_EOL, FILE_APPEND | LOCK_EX);
        } else {
            file_put_contents((string)parent::xmlFileHandler(new String('App/Config/config.xml'), new String('logfiledir')).$log_file_name.'.txt', '['.date('d-m-Y H:i:s').'] '.$data.PHP_EOL.$backtrace.PHP_EOL.PHP_EOL, LOCK_EX);
        }
    }
}